package solution;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution {
    public ListNode swapPairs(ListNode head) {

        if (null == head || null == head.next) {
            return head;
        }

        ListNode newHead = new ListNode();
        newHead.next = head;

        ListNode left = newHead;
        ListNode mid = head;
        ListNode right = head.next;

        while (left != null && right != null && mid != null) {
            mid.next = right.next;
            right.next = mid;
            left.next = right;

            left = mid;
            if (left != null) {
                mid = left.next;
            }
            if (mid != null) {
                right = mid.next;
            }
        }

        return newHead.next;

    }
}